package com.neusoft.tijiancms.datastruc;

import java.util.Stack;

/**
 * 使用数组实现入栈和出栈
 *
 */
public class MyStack {
    private int[] array;
    private int top;
    private int maxSize;

    public MyStack(int maxSize) {
        this.maxSize = maxSize;
        this.top = -1;
        this.array = new int[maxSize];
    }

    //压入数据
    public void push(int value) {
        if(top < maxSize - 1) {
            array[++top] = value;
        }
    }

    //弹出数据
    public int pop() {
        return array[top--];
    }

    //访问栈顶数据
    public int peek() {
        return array[top];
    }

    //判断栈是否为空
    public boolean isEmpty() {
        return top == -1;
    }

    //判断栈是否已经满了
    public boolean isFull() {
        return top == maxSize -1;
    }

    public static void main(String[] args) {
//        MyStack myStack = new MyStack(3);
//        myStack.push(1);
//        myStack.push(2);
//        myStack.push(3);
//        myStack.push(4);
//        System.out.println(myStack.peek());
//        System.out.println(myStack.isFull());
//        System.out.println("----------------------------------");
//        while (!myStack.isEmpty()) {
//            System.out.println(myStack.pop());
//        }

        //将字符串(how are you)反转  集合中提供了 Stack
        Stack stack = new Stack();
        String str = "how are you";
        char[] cha = str.toCharArray();
        for(char c : cha){
            stack.push(c);
        }
        while(!stack.isEmpty()){
            System.out.print(stack.pop());
        }
    }
}
